﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccessLayer.Models;

namespace PresentationLayerMVC.Controllers
{
    public class ProductController : BaseController
    {
        //
        // GET: /Product/
        [OutputCache(Duration=60, VaryByParam="none")]
        public ActionResult Index(int? id)
        {
            //LOSEntities ctx = new LOSEntities();
            //var model = ctx.Products.AsEnumerable();

            //if (id != null)
            //{
            //    var prod = (from p in ctx.Products
            //                where p.ProductId == id
            //                select p).SingleOrDefault();

            //    PutProductInSession(prod);
            //    ViewData["Product"] = prod;
            //}

            //return View(model);

            initDisplay();

            if (id != null)
            {
                int prodId = (int)id;
                Product prod = ProductFactory.getInstance().getProduct(prodId);
                PutProductInSession(prod);
                ViewData["Product"] = prod;
            }

            return View(ProductFactory.getInstance().getProducts());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            IValueProvider info = collection.ToValueProvider();
            PutAppModelInSession(INFO_FAC, info);

            if (GetProductFromSession() != null)
            {
                Product prod = GetProductFromSession();

                if (prod is HousingLoan)
                    return Next(Constants.ACT_ADDFACILITY_HL);
                else if (prod is TermLoan)
                    return Next(Constants.ACT_ADDFACILITY_TL);
                else if (prod is OverDraft)
                    return Next(Constants.ACT_ADDFACILITY_OD);
            }

            return View();
        }
    }
}
